package com.xhui.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhui.reggie.common.BaseContext;
import com.xhui.reggie.common.R;
import com.xhui.reggie.entity.ShoppingCart;
import com.xhui.reggie.service.ShoppingCartService;
import com.xhui.reggie.mapper.ShoppingCartMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 王辉
 * @description 针对表【shopping_cart(购物车)】的数据库操作Service实现
 * @createDate 2023-01-20 15:25:14
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
        implements ShoppingCartService {

    /**
     * 添加菜品
     * @param shoppingCart 总金额 菜品口味(套餐暂无) 图片 套餐/菜品名称 套餐/菜品id
     * @return 待添加菜品操作完之后的信息
     */
    @Override
    @Transactional
    public R<ShoppingCart> add(ShoppingCart shoppingCart) {

        // 设置用户的Id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        // 查询 套餐/菜品 是否已存在表 shopping_cart 中
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        // 若查询到，在原有数量 + 1,只更新，不新增
        // 未查询到，新增数据
        if (shoppingCart.getDishId() != null) {
            // 添加的是菜品，并且不同口味多条记录
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            // 由于前端没有对 一样菜品&不同口味 进行区分，故此处不对口味进行限制
            // queryWrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());


        } else {
            // 添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // select * from shoppingCart  where user_id = ? and  dish_id/setmeal_id = ?
        ShoppingCart queriedShoppingCart = this.getOne(queryWrapper);

        if (queriedShoppingCart != null) {
            // 待添加的已存在，在原有数量上 + 1
            queriedShoppingCart.setNumber(queriedShoppingCart.getNumber() + 1);
            this.updateById(queriedShoppingCart);

        } else {
            // 待添加的不存在，进行新增，且默认数量为1
            shoppingCart.setNumber(1);
            this.save(shoppingCart);
            queriedShoppingCart = shoppingCart;
        }

        return R.success(queriedShoppingCart);
    }

    /**
     * 购物车 菜品/套餐 - 1
     * @param shoppingCart 菜品/套餐 的id
     * @return 待删除菜品 -1 后的最新数据
     */
    @Override
    @Transactional
    public R<ShoppingCart> sub(ShoppingCart shoppingCart) {
        // 设置用户的Id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        // 查询 套餐/菜品 是否已存在表 shopping_cart 中
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        //
        //
        if (shoppingCart.getDishId() != null) {
            // 减少菜品，
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());

        } else {
            // 添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // select * from shoppingCart  where user_id = ? and  dish_id/setmeal_id = ? and dish_flavor = ?
        // select * from shoppingCart  where user_id = ? and  setmeal_id = ?
        ShoppingCart queriedShoppingCart = this.getOne(queryWrapper);

        if (queriedShoppingCart.getNumber() > 1) {
            // 待减去的已存在 > 一个，在原有数量上 - 1
            queriedShoppingCart.setNumber(queriedShoppingCart.getNumber() - 1);
            this.updateById(queriedShoppingCart);

            // 操作完之后，再次查询被操作的指定 菜品/套餐 信息，确保最新
            return R.success(this.getOne(queryWrapper));
        }else {
            // 待减去的存在 1 个，进行减去，执行删除
            this.remove(queryWrapper);
            ShoppingCart cart = new ShoppingCart();
            // 返回给前端设置待删除的菜品
            cart.setNumber(0);
            return R.success(cart);
        }

    }

    /**
     * 清空购物车
     */
    @Override
    @Transactional
    public boolean delete() {
        // delete from shopping_cart where and user_id = ?
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        return this.remove(wrapper);
    }
}



